package com.data.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.config.dao.EquStateConfigDao;
import com.config.dao.EquipmentDao;
import com.config.dao.LineConfigDao;
import com.config.entity.EquStateConfig;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.data.dao.StateDayDao;
import com.data.dao.StateWeekDao;
import com.data.entity.StateDay;
import com.data.entity.StateWeek;

@Service("stateWeekService")
public class StateWeekServiceImpl  implements StateWeekService{
	@Resource(name="lineConfigDao")
	private LineConfigDao lineConfigDao;
	@Resource(name="equipmentDao")
	private EquipmentDao equipmentDao;
	@Resource(name="stateWeekDao")
	private StateWeekDao stateWeekDao;
	@Resource(name="stateDayDao")
	private StateDayDao stateDayDao;
	@Resource(name="equStateConfigDao")
	private EquStateConfigDao equStateConfigDao;
	/**
	 * 根据日期提取设备每周状态记录
	 * @param belongDate
	 */
	@Override
	public void saveStateWeek(Date belongDate) {
		SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Calendar ca=Calendar.getInstance();
		ca.setTime(belongDate);
		int belongWeek=ca.get(Calendar.WEEK_OF_YEAR);
		int belongYear=Integer.parseInt(sdf.format(belongDate));
		List<LineConfig> lines=lineConfigDao.findAllLine();
		List<EquStateConfig> states=equStateConfigDao.findAll();
		if(lines.size()!=0){
			for(LineConfig line:lines){
				List<Equipment> equs=equipmentDao.listByLine(line.getId());
				if(equs.size()!=0){
					for(Equipment e:equs){
						if(states.size()!=0){
							for(EquStateConfig state:states){
								StateWeek week=stateWeekDao.getStateWeekByStateAndDate(e.getId(), belongYear, belongWeek, state.getStateType());
								boolean isNew=false;
								if(week==null){
									week=new StateWeek();
									week.setEquId(e.getId());
									week.setBelongYear(belongYear);
									week.setBelongWeek(belongWeek);
									week.setStateType(state.getStateType());
									isNew=true;
								}
								Calendar  cal=Calendar.getInstance();
								cal.set(Calendar.YEAR, belongYear);//设置年份
								cal.set(Calendar.WEEK_OF_YEAR,belongWeek);//设置第几周
								cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);//设置周一为第一天
								String startDate=sdft.format(cal.getTime());//某一周的开始日期
								String endDate=sdft.format(belongDate);
								
								List<StateDay> days=stateDayDao.getStateDays(e.getId(), state.getStateType(), startDate, endDate);
								double duration=0.0; double relativeComp=0.0;
								if(days.size()!=0){
									for(StateDay day:days){
										duration+=day.getDuration();
										relativeComp+=day.getRelativeComp();
									}
								}
								week.setRelativeComp(relativeComp);
								week.setDuration(duration);
								if(isNew){
									stateWeekDao.save(week);
								}else{
									stateWeekDao.update(week);
								}
							}
						}
					}
				}
			}
		}
	}
}
